#include "apue.h"
#include <sys/wait.h>

char *env_init[] = {"USER=unknow", "PATH=/tmp", NULL};

int main(int argc, char *argv[]) {
    pid_t pid;

    if((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {
        if (execle("/tmp/8-16", "8-16_echoall", "myargl",
                   "MY ARG2", (char *)0, env_init) < 0) {
            err_sys("execle error");
        }
    }

    if (waitpid(pid, NULL, 0) < 0) {
        err_sys("wait error");
    }

    const char *path = getenv("PATH");
    char buf[BUFSIZ];
    strncpy(buf, path, sizeof(buf));
    const char new_path_component[] = ":/tmp";
    strncat(buf, new_path_component, strlen(new_path_component) + 1);
    setenv("PATH", buf, 1);

    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {
        if (execlp("8-16_echoall", "echoall", "only 1 arg", (char *)0) < 0) {
            err_sys("execlp error");
        }
    }
    return 0;
}